package com.mateng.treasurechest.view.recipe.activity

import android.os.Bundle
import com.bumptech.glide.Glide
import com.mateng.treasurechest.R
import com.mateng.treasurechest.base.BaseActivity
import com.mateng.treasurechest.model.recipe.bean.RecipeDetailsBean
import com.mateng.treasurechest.presenter.recipe.RecipePresenter
import com.mateng.treasurechest.view.recipe.EXTRAS
import com.mateng.treasurechest.view.recipe.glideOptions
import com.mateng.treasurechest.view.recipe.interfaces.IRecipeDetailsView
import kotlinx.android.synthetic.main.activity_recipe_details.*
import kotlinx.android.synthetic.main.item_grid_main.*

/**
 * Created by mateng on 2018/1/27.
 */
class RecipeDetailsActivity : BaseActivity<RecipePresenter>(), IRecipeDetailsView {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recipe_details)

        showBackIcon()

        presenter?.iRecipeDetailsView = this
        presenter?.getRecipeDetails(intent.getStringExtra(EXTRAS.KEY_ID))
        tv_title.text = intent.getStringExtra(EXTRAS.KEY_NAME)
        showProgress()
    }

    override fun getRecipeDetailsDataSuccess(recipeDetailsBean: RecipeDetailsBean) {
        hideProgress()
        if (recipeDetailsBean.isSuccess()) {
            tv_name.text = recipeDetailsBean.result?.name
            tv_sumary.text = recipeDetailsBean.result?.recipe?.sumary?.trim()
            recipeDetailsBean.result?.recipe?.getIngredients()?.forEach {
                tv_ingredients.append(it + "\n")
            }
            recipe_step.setData(recipeDetailsBean.result?.recipe?.method)

            Glide.with(this).applyDefaultRequestOptions(glideOptions).load(recipeDetailsBean.result?.recipe?.img).into(iv_recipe_img)
        }
    }

    override fun requestError(throwable: Throwable) {
        netConnectFailed()
    }

}